package com.bledanliaar;

import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.bledanliaar.measure.precentor.OnBledataMeasureListener;
import com.bledanliaar.measure.precentor.Precentor;
import com.blelibrary.BleOperate;
import com.blelibrary.SearchBleMacResultListener;
import com.utils.LogUtils;

import java.util.ArrayList;


public class MainActivity extends AppCompatActivity{
//    "00:15:83:00:73:D5"
//    00:15:83:00:71:08
   BleOperate  bleOperate;
    public  String  macAddress ="00:15:83:00:73:D5";
    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        LogUtils.setDEBUG(true);
        bleOperate=  BleOperate.getInstance();
        if (bleOperate.initialize(this)){
        } else {
            LogUtils.e("蓝牙不可用");
        }
        Button btn_send = (Button) findViewById(R.id.btn_send);
        btn_send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                byte[] inits = {(byte) 0x88};
                bleOperate.writeCharacteristic(macAddress, inits);
            }
        });
        Button btn_send2 = (Button) findViewById(R.id.btn_send2);
        final EditText       editText=(EditText)findViewById(R.id.editText_command);
        btn_send2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


            }
        });
        Button btn_handler = (Button) findViewById(R.id.btn_handler);
        btn_handler.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Precentor  pt=new Precentor(new Handler());
                pt.measureBleData("00:15:83:00:73:D5", new OnBledataMeasureListener() {
                    @Override
                    public void result(ArrayList<Integer> result) {
                        LogUtils.e("集合大小："+result.size());

                    }
                    @Override
                    public void error() {
                        LogUtils.e("获取数据失败");
                    }
                });
            }
        });
        findViewById(R.id.disconnect).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BleOperate.getInstance().disconnect("00:15:83:31:1C:88");
            }
        });
        findViewById(R.id.connect).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                connectCardReader();
            }
        });

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (bleOperate!=null){
            bleOperate.disconnect();
            LogUtils.e("关闭蓝牙连接");
        }
    }

       SearchBleMacResultListener    searchBleMacResultListener= new SearchBleMacResultListener() {
        @Override
        public void hasConnected() {

        }
        @Override
        public void searchResult(boolean value) {
            if (value) {
                LogUtils.e( "指定设备连接成功");
            } else {
                LogUtils.e("指定设备连接失败");
            }
        }

        @Override
        public void findUUIDService() {
        }
        @Override
        public void findUUIDNotify() {
        }

        @Override
        public void timeOut() {
        }
        @Override
        public void progress(long millisecond) {
        }
        @Override
        public void onResultReceive(byte[] result) {
            if(result!=null&&result.length>=2){
                byte []  newArra=new  byte[2];
                System.arraycopy(result,1,newArra,0,2);
                LogUtils.e("数据校验成功-》"+result.length+"_"+bytes2short(newArra));
            }
            LogUtils.e("通信蓝牙返回数据-》"+new  String(result));
            connectCardReader();
        }

           @Override
           public void disconect(String address) {

           }

       };

    public  void  connectCardReader(){
        BleOperate.getInstance().connectBleDeviceByMac("00:15:83:31:1C:88",
                new SearchBleMacResultListener() {
                    @Override
                    public void hasConnected() {

                    }

                    @Override
                    public void searchResult(boolean value) {
                        if (value) {
                            Log.e("TAG", "指定设备连接成功");
                        } else {
                            Log.e("TAG", "指定设备连接失败");
                        }
                    }

                    @Override
                    public void findUUIDService() {
                    }
                    @Override
                    public void findUUIDNotify() {
                        LogUtils.e("读卡器已连接");
                    }

                    @Override
                    public void timeOut() {

                    }
                    @Override
                    public void progress(long millisecond) {

                    }

                    @Override
                    public void onResultReceive(byte[] result) {
                        LogUtils.e("读卡器返回数据-》"+new  String(result));

                    }

                    @Override
                    public void disconect(String address) {

                    }
                });
    }
    public static String toHex(byte[] buffer) {
        StringBuffer sb = new StringBuffer(buffer.length * 2);
        for (int i = 0; i < buffer.length; i++) {
            sb.append(Character.forDigit((buffer[i] & 240) >> 4, 16));
            sb.append(Character.forDigit(buffer[i] & 15, 16));
        }
        return sb.toString();
    }



    /* *
     * Convert byte[] to hex string.这里我们可以将byte转换成int，然后利用Integer.toHexString(int)
     *来转换成16进制字符串。
     * @param src byte[] data
     * @return hex string
     */
    public static String bytesToHexString(byte[] src){
        StringBuilder stringBuilder = new StringBuilder("");
        if (src == null || src.length <= 0) {
            return null;
        }
        for (int i = 0; i < src.length; i++) {
            int v = src[i] & 0xFF;
            String hv = Integer.toHexString(v);
            if (hv.length() < 2) {
                stringBuilder.append(0);
            }
            stringBuilder.append(hv);
        }
        return stringBuilder.toString();
    }
    public static short bytes2short(byte[] b) {
        return (short) (b[1] & 0xff | (b[0] & 0xff) << 8);
    }

}
